//
//  FullViewController.m
//  网络视频播放(AVPlayer)
//
//  Created by apple on 15/8/16.
//  Copyright (c) 2015年 FMG. All rights reserved.
//

#import "FullViewController.h"
#import "FullView.h"

#import "AppDelegate.h"


@interface FullViewController ()

@end

@implementation FullViewController

- (void)loadView
{
    FullView *fullView = [[FullView alloc] init];
    self.view = fullView;

}

-(void)addBackButton
{
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _backBtn.frame = CGRectMake(10, 35, 72, 25);
    //button_back  back.png
    UIImageView *backImg = [[UIImageView alloc]
                            initWithImage:[UIImage imageNamed:@"mybuttonback"]];
    backImg.frame = CGRectMake(0, 0, 25, 25);
    [_backBtn addSubview:backImg];
    [_backBtn addTarget:self
                 action:@selector(full_leftBarButtonItemPressed:)
       forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backBtn];
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

/**
 *返回按钮事件
 */
-(void)full_leftBarButtonItemPressed:sender
{
    //先通知代理将视频控件添加回界面，然后 退出当前全屏界面
    [self.delegate restoreView];
    [self setNewOrientation:NO];
    [AppDelegate sharedAppDelegate].allowRotation = NO;
    [self.navigationController popViewControllerAnimated:YES];
}


// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
    return NO;
}


- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator{
    NSLog(@"FullViewController------------------------");
    //当从横屏翻转到竖屏时，退出全屏，回到原始界面。
    //注：回原始界面前，先将视频控件还原到原界面，再退出全屏界面
    if(self.interfaceOrientation > UIInterfaceOrientationPortrait) {
        [self.delegate restoreView];
        [self.navigationController popViewControllerAnimated:YES];
       
    }
}

-(void)viewDidAppear:(BOOL)animated
{
    [self.view bringSubviewToFront:self.backBtn];
}

/**
 *
 *底层切换横竖屏
 */
- (void)setNewOrientation:(BOOL)fullscreen
{
    if (fullscreen) {
        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
        [AppDelegate sharedAppDelegate].allowRotation = NO;
    }else{
        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
        [AppDelegate sharedAppDelegate].allowRotation = NO;
    }
}



@end
